package com.liy.supercloud.ssoserver.service.impl;

import com.liy.framework.common.extvo.ResultVO;
import com.liy.framework.common.extvo.TokenVo;
import com.liy.framework.common.redis.utils.RedisUtils;
import com.liy.framework.common.utils.Tools;
import com.liy.supercloud.ssoserver.service.AuthorizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 授权服务
 */
@Service
public class AuthorizationServiceImpl implements AuthorizationService {

    @Autowired
    private RedisUtils redisUtils;

    public ResultVO authorization(String token) {
        return this.validatorToken(token);
    }

    public ResultVO hasPermission(String token, String codes) {
        ResultVO resultVO = this.validatorToken(token);
        if (!resultVO.isSuccess()) {
            return resultVO;
        } else {
            TokenVo tokenVo = (TokenVo)resultVO.getData();
            String[] arr = codes.split(";");
            String[] var6 = arr;
            int var7 = arr.length;

            for(int var8 = 0; var8 < var7; ++var8) {
                String var10000 = var6[var8];
            }

            resultVO.setSuccess(true);
            return resultVO;
        }
    }

    /*
    * 校验 Token
    */
    private ResultVO validatorToken(String token) {
        ResultVO resultVO = new ResultVO();
        if (Tools.StringIsNullOrSpace(token)) {
            resultVO.setSuccess(false);
            resultVO.setMessage("请求信息不完整.");
            return resultVO;
        } else {
            TokenVo tokenVo = (TokenVo)this.redisUtils.get(token.trim());
            if (tokenVo == null) {
                resultVO.setSuccess(false, "401");
                resultVO.setMessage("未认证或已过期.");
                return resultVO;
            } else if (tokenVo.getStatus() != 0) {
                resultVO.setSuccess(false, "403");
                resultVO.setMessage("认证已被禁用.");
                return resultVO;
            } else {
                resultVO.setSuccess(true, "200");
                resultVO.setMessage("通过认证");
                resultVO.setData(tokenVo);
                return resultVO;
            }
        }
    }
}
